/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.dianning.dao.hibernate;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dianning.bean.AdAds;
import com.dianning.bean.AdSpace;
import com.dianning.bean.Admin;
import com.dianning.bean.Area;
import com.dianning.bean.Channel;
import com.dianning.bean.Credit;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.Util;
import com.dianning.dao.AdAdsDAO;
import com.dianning.dao.AdSpaceDAO;
import com.dianning.dao.AdminDAO;
import com.dianning.dao.AreaDAO;
import com.dianning.dao.ChannelDAO;
import com.dianning.dao.CreditDAO;
import com.dianning.dao.VisitorDAO;

/**
 * 功能描述:奖励记录操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class CreditHibernateDAO extends HibernateDaoSupport implements CreditDAO {

	private static final String LOAD_BY_ID = "from Credit where id = ?";
	private static final String LOAD_BY_UID_CODE = "from Credit where uid = ? and code = ?";
	private static final String LOAD_BY_UID_CODE_TIME = "from Credit where uid = ? and code = ? and addTime<=dateadd(hh,-6,getdate())";
	private static final String LOAD_BY_UID_CODE_DATE = "from Credit where uid = ? and code = ? and CONVERT(varchar(12),addTime,111)=CONVERT(varchar(12),getdate(),111)";
	
	/**
	 * 保存Credit对象
	 * 
	 * @param credit
	 * 			Credit对象
	 * @return Credit
	 * 			Credit对象
	 */
	public Credit saveCredit(Credit credit){
		this.getHibernateTemplate().save(credit);
		return credit;
	}
	/**
	 * 保存或更新Credit对象
	 * 
	 * @param credit
	 * 			Credit对象
	 * @return Credit
	 * 			Credit对象
	 */
	public Credit saveOrUpdateCredit(Credit credit){
		this.getHibernateTemplate().saveOrUpdate(credit);
		return credit;
	}
	/**
	 * 保存Credit对象
	 * 
	 * @param credit
	 * 			Credit对象
	 * @return Credit
	 * 			Credit对象
	 */
	public Credit updateCredit(Credit credit){
		this.getHibernateTemplate().update(credit);
		return credit;
	}
	
	/**
	 * 根据主键ID获取Credit对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Credit
	 * 			Credit对象
	 */
	public Credit findCreditById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Credit) l.get(0);
		}
	}
	
	/**
	 * 获取Credit个数
	 * 
	 * @return int
	 * 			Credit个数
	 */
	public long getCreditNum(){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Credit.class);
				c.setProjection(Projections.count("id"));
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Long) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取Credit列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Credit对象列表
	 */
	public List findCredits(final String orderby, final int ascOrDesc, final int firstResult, final int maxResults){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Credit.class);
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				c.setFirstResult(firstResult);
				c.setMaxResults(maxResults);
				return c.list();
			}
		});
	}
	
	/**
	 * 根据code获取过期一天Credit对象
	 * @param code
	 * @return Credit
	 */
	public Credit findCreditByUidCodeTime(long uid,String code){
		Object[] o = {new Long(uid),code};
		List l = this.getHibernateTemplate().find(LOAD_BY_UID_CODE_TIME,o);
		if (l == null || l.isEmpty()) {
			return null;
		} else {
			return (Credit) l.get(0);
		}
	}
	/**
	 * 根据code获取过期一天Credit对象
	 * @param code
	 * @return Credit
	 */
	public Credit findCreditByUidCodeDate(long uid,String code){
		Object[] o = {new Long(uid),code};
		List l = this.getHibernateTemplate().find(LOAD_BY_UID_CODE_DATE,o);
		if (l == null || l.isEmpty()) {
			return null;
		} else {
			return (Credit) l.get(0);
		}
	}
	/**
	 * 根据code获取Credit对象
	 * @param code
	 * @return Credit
	 */
	public Credit findCreditByUidCode(long uid,String code){
		Object[] o ={new Long(uid),code};
		List l = this.getHibernateTemplate().find(LOAD_BY_UID_CODE,o);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Credit) l.get(0);
		}
	}
}
